﻿using GalaSoft.MvvmLight;
using System.Diagnostics;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;

namespace SimpleJackInterface.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ChatLogViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the ChatLogViewModel class.
        /// </summary>
        public ChatLogViewModel(MainViewModel mvm)
        {
            this.ParentMainViewModel = mvm;
            
            ChatLogItems.Add(new ChatLogItemViewModel(eChatter.SimpleJack, "hallo"));
            ChatLogItems.Add(new ChatLogItemViewModel(eChatter.User, "hoi"));
            ChatLogItems.Add(new ChatLogItemViewModel(eChatter.SimpleJack, "bla"));
            ChatLogItems.Add(new ChatLogItemViewModel(eChatter.User, "bla!"));
            ChatLogItems.Add(new ChatLogItemViewModel(eChatter.SimpleJack, "more bla"));

            AddChatLogItemCommand = new RelayCommand<ChatLogItemViewModel>(new System.Action<ChatLogItemViewModel>(AddChatLogItem));

            if (IsInDesignMode)
            {
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        private void AddChatLogItem(ChatLogItemViewModel chatLogItem)
        {
            this.ChatLogItems.Add(chatLogItem);
        }
        
        #region ParentMainViewModel Property
        /// <summary>
        /// The <see cref="ParentMainViewModel" /> property's name.
        /// </summary>
        public const string ParentMainViewModelPropertyName = "ParentMainViewModel";

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private MainViewModel _ParentMainViewModel = null;

        /// <summary>
        /// Gets the ParentMainViewModel property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public MainViewModel ParentMainViewModel
        {
            get
            {
                return _ParentMainViewModel;
            }

            set
            {
                if (_ParentMainViewModel == value)
                {
                    return;
                }

                var oldValue = _ParentMainViewModel;
                _ParentMainViewModel = value;

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(ParentMainViewModelPropertyName, oldValue, value, true);
            }
        }
        #endregion


        #region ChatLogItems Property
        /// <summary>
        /// The <see cref="ChatLogItems" /> property's name.
        /// </summary>
        public const string ChatLogItemsPropertyName = "ChatLogItems";

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private ObservableCollection<ChatLogItemViewModel> _chatLogItems = new ObservableCollection<ChatLogItemViewModel>();

        /// <summary>
        /// Gets the ChatLogItems property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<ChatLogItemViewModel> ChatLogItems
        {
            get
            {
                return _chatLogItems;
            }

            set
            {
                if (_chatLogItems == value)
                {
                    return;
                }

                var oldValue = _chatLogItems;
                _chatLogItems = value;

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(ChatLogItemsPropertyName, oldValue, value, true);
            }
        }
        #endregion

        
        #region AddChatLogItemCommand Property
        /// <summary>
        /// The <see cref="AddChatLogItemCommand" /> property's name.
        /// </summary>
        public const string AddChatLogItemCommandPropertyName = "AddChatLogItemCommand";

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private RelayCommand<ChatLogItemViewModel> _AddChatLogItemCommand = null;

        /// <summary>
        /// Gets the AddChatLogItemCommand property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public RelayCommand<ChatLogItemViewModel> AddChatLogItemCommand
        {
            get
            {
                return _AddChatLogItemCommand;
            }

            set
            {
                if (_AddChatLogItemCommand == value)
                {
                    return;
                }

                var oldValue = _AddChatLogItemCommand;
                _AddChatLogItemCommand = value;

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(AddChatLogItemCommandPropertyName, oldValue, value, true);
            }
        }
        #endregion


    }
}